package nl.nhl.idp.Views;

import java.io.IOException;

import nl.nhl.idp.R;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

public class BarView extends View
{
	private boolean mirrored;
	
	private int value;
	
	private Paint outlinePaint;
	private Paint fillPaint;
	private Paint iconPaint;
	private RectF outlineRect;
	private Bitmap icon;
	
	public void SetValue(int value)
	{
		//set value
		this.value = value;
		
		//clip value between 0 and 100
		if(value > 100)
			value = 100;
		else if(value < 0)
			value =0;
		
		//get color values based on value
		int grn = (int)(value/100.0f*510);
		int red = 510-(int)(value/100.0f*510);
		
		//clip color values
		if(grn > 255)
			grn = 255;
		if(red > 255)
			red = 255;
		
		//set fill color
		fillPaint.setColor(Color.rgb(red, grn, 0));
		invalidate();
	}
	
	public void SetMirrored(boolean mirrored)
	{
		this.mirrored = mirrored;
		if(this.mirrored)
		{
			outlineRect = new RectF(0, 0, this.getWidth()*2, this.getHeight()*2);
		}
		else
		{
			outlineRect = new RectF(-this.getWidth(), 0, this.getWidth(), this.getHeight()*2);
		}
	}
	
	public BarView(Context context)
	{
		this(context, null);
	}
	
	public BarView(Context context, AttributeSet attrs)
	{
		this(context, attrs, 0);
	}
	
	public BarView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		
		// check if we are using attributes
		if(attrs != null)
		{
			// get typedArray of attributes
			TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BarViewAttr);
			
			// get the attributes
			mirrored = a.getBoolean(R.styleable.BarViewAttr_mirrored, false);
		}
		else
		{
			mirrored = true;
		}
		
		initView(context.getAssets());
	}

	private void initView(AssetManager assets)
	{
		if(!this.isInEditMode())
		{
			try
			{
				if(mirrored)
					icon = BitmapFactory.decodeStream(assets.open("images/engine_voltage.png"));
				else
					icon = BitmapFactory.decodeStream(assets.open("images/battery_voltage.png"));
				icon = Bitmap.createScaledBitmap(icon, 32, 32, false);
				
			}
			catch(IOException ioe){}
		}
		
		outlinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		outlinePaint.setColor(Color.DKGRAY);
		outlinePaint.setStrokeWidth(3);
		outlinePaint.setStyle(Style.STROKE);
		
		fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		fillPaint.setColor(Color.GREEN);
		fillPaint.setStyle(Style.FILL);
		
		iconPaint = new Paint();
		
		outlineRect = new RectF(0, 0, 40, 100);
		
		value = 0;
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		if(!this.isInEditMode())
		{
			if(mirrored)
			{
				outlineRect = new RectF(0, 0, this.getWidth()*2, this.getHeight()*2);
			}
			else
			{
				outlineRect = new RectF(-this.getWidth(), 0, this.getWidth(), this.getHeight()*2);
			}
		}
		else
		{
			outlineRect = new RectF(0, 0, this.getWidth(), this.getHeight());
		}
		
		setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		if(!this.isInEditMode())
		{
			int val = (int)(90f/100f*value);
			if(mirrored)
			{
				canvas.drawArc(outlineRect, 180, val, true, fillPaint);
				canvas.drawArc(outlineRect,  180, 90, false, outlinePaint);
				canvas.drawBitmap(icon, 8, this.getHeight()-40, iconPaint);
			}
			else
			{
				canvas.drawArc(outlineRect, 360-val, val, true, fillPaint);
				canvas.drawArc(outlineRect, 270, 90, false, outlinePaint);
				canvas.drawBitmap(icon, this.getWidth()-40, this.getHeight()-40, iconPaint);
			}
		}
		else
		{
			canvas.drawRect(outlineRect, outlinePaint);
		}
	}
}
